﻿using SD.Entity;
using SD.Entity.Jsons;
using SD.Entity.PageSearch;
using SD.Entity.SearchEntity;
using SD.Interface.Service;
using SD.UI.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SD.UI.Controllers
{
    public class UserInfoController : Controller
    {
        //
        // GET: /UserInfo/
        private IUserInfoService _userInfoService;

        public UserInfoController(IUserInfoService userInfoService)
        {
            _userInfoService = userInfoService;
        }

        [MyAuthorization]
        public ActionResult Index()
        {
            return View();
        }

        public JsonResult UserInfoList(UserInfoSearch search, FormCollection form)
        {
            PageView view = new PageView(form);
            string colkey = form["colkey"];
            string colsinfo = form["colsinfo"];
            PagedList<UserInfo> list = _userInfoService.QueryUserInfoList(search, view);
            JsonQTable at = JsonQTable.ConvertFromPagedList<UserInfo>(list, colkey, colsinfo.Split(','));
            return Json(at);
        }

        [MyAuthorization]
        public ActionResult UserInfoEdit(int? id)
        {
            UserInfo userInfo = null;
            if (id.HasValue)
            {
                userInfo = _userInfoService.GetUserInfo(id.Value);
            }
            else
            {
                userInfo = new UserInfo();
            }

            List<SelectListItem> list = new List<SelectListItem>();
            list.Add(new SelectListItem { 
                Selected = false,
                Text = "正常使用",
                Value="0"
            });
            list.Add(new SelectListItem { 
                Selected=true,
                Text = "已删除",
                Value="1"
            });
            ViewBag.List = list;
            return View(userInfo);
        }

        public JsonResult UserInfoSave(UserInfo userInfo)
        {
            JsonSMsg rMsg = new JsonSMsg();
            try
            {
                userInfo.ModifiedOn = DateTime.Now;
                int ret = (int)_userInfoService.SaveUserInfo(userInfo);
                if (ret > 0)
                {
                    rMsg.Status = 1;
                    rMsg.Message = "操作成功！";
                }
                else
                {
                    rMsg.Status = -1;
                    rMsg.Message = "意外错误，请稍后重试！";
                }
            }
            catch (Exception ex)
            {
                rMsg.Status = -1;
                rMsg.Message = ex.Message;
            }
            return Json(rMsg);
        }

        [MyAuthorization]
        public JsonResult UserInfoDelete(int id)
        {
            JsonSMsg rMsg = new JsonSMsg();
            rMsg.Status = 1;
            rMsg.Message = "模拟操作成功！";
            return Json(rMsg);
        }

    }
}
